import {
  articles,
  channels,
  userChannels,
  userChannelsDelete,
  userChannelsPatch
} from '@/api'
import { Channel } from '@/types/data'
import { RootThunkAction, UserChannelType } from '@/types/store'
import { getChannels, hasToken, setChannels } from '@/utils/auth'

// 用户频道 action
function channelAction (): RootThunkAction {
  return async dispatch => {
    if (hasToken()) {
      const res = await userChannels()

      console.log(res)
      dispatch({ type: 'user/channel', payload: res.data.data.channels })
    } else {
      const channelAll = getChannels()
      if (channelAll.length > 0) {
        dispatch({ type: 'user/channel', payload: channelAll })
      } else {
        const res = await channels()
        setChannels(res.data.data.channels)
        dispatch({ type: 'user/channel', payload: res.data.data.channels })
      }
    }
  }
}
// 所有频道 action
function allChannelAction (): RootThunkAction {
  return async dispatch => {
    const res = await channels()
    dispatch({ type: 'all/Channel', payload: res.data.data.channels })
  }
}
// 修改activeId的 action
function setActiveIdAction (id: number): UserChannelType {
  return { type: 'activeId/set', payload: id }
}
// 添加用户频道 action
function addChannelAction (id: number): RootThunkAction {
  return async (dispatch, getState) => {
    if (hasToken()) {
      await userChannelsPatch(id)
    } else {
      const { allChannel, userChannel } = getState().channelReducer
      setChannels([
        ...userChannel,
        allChannel.find(v => v.id === id) as Channel
      ])
    }
    dispatch(channelAction())
  }
}
// 删除用户频道 action
function delChannelAction (id: number): RootThunkAction {
  return async (dispatch, getState) => {
    if (hasToken()) {
      await userChannelsDelete(id)
      dispatch(channelAction())
    } else {
      const { userChannel } = getState().channelReducer
      setChannels(userChannel.filter(v => v.id !== id))
    }
    dispatch(channelAction())
  }
}
// 获取文章的action
function getArticleAction (
  channel_id: number,
  pre_timestamp: number
): RootThunkAction {
  return async dispatch => {
    const res = await articles(channel_id, pre_timestamp)
    console.log(res.data.data.results)
    dispatch({
      type: 'article/channel',
      payload: {
        channelId: channel_id,
        articles: res.data.data.results,
        pre_timestamp: res.data.data.pre_timestamp
      }
    })
  }
}
// 获取文章的action
function refArticleAction (channel_id: number): RootThunkAction {
  return async dispatch => {
    const res = await articles(channel_id, Date.now())
    console.log(res.data.data.results)
    dispatch({
      type: 'refrech/article',
      payload: {
        channelId: channel_id,
        articles: res.data.data.results,
        pre_timestamp: res.data.data.pre_timestamp
      }
    })
  }
}
export {
  channelAction,
  allChannelAction,
  setActiveIdAction,
  addChannelAction,
  delChannelAction,
  getArticleAction,
  refArticleAction
}
